#Flexbox Layout in CSS
Previously, the elements we placed on a webpage were arranged in the order they were written, and block elements always occupied an entire line, which limited our control.
For example, in the Semantic HTML section, the aside
element was used as a sidebar, but it appeared on its own line instead of being placed to the side.
In this section, we’ll learn about Flexbox, one of the most commonly used layout techniques in CSS. Flexbox allows us to easily control the alignment, distribution, and ordering of elements inside a container.
By setting an element’s display
property to flex
, the element becomes a flex container, and its immediate child elements no longer occupy an entire line.
Example:
<div style="display:flex; height:100px;">
<main style="background-color:#212121; color:cyan;">
main
</main>
<aside style="background-color:#666; color:yellow;">
aside
</aside>
</div>
HTML
main
#Main Axis and Cross Axis
In a flex container, the default main axis is horizontal, and the cross axis is vertical. Child elements are arranged along the main axis.
You can change the direction using the container’s flex-direction
property:
row
– Horizontal is the main axis; vertical is the cross axiscolumn
– Vertical is the main axis; horizontal is the cross axis
Example:
<div style="display:flex; flex-direction:column; height:100px;">
<main style="background-color:#212121; color:cyan;">
main
</main>
<aside style="background-color:#666; color:yellow;">
aside
</aside>
</div>
HTML
main
#flex-grow and flex-shrink
When the total length of child elements is less than the container’s main axis, you can use flex-grow
to make them grow to fill the space.
Example:
<div style="display:flex; height:100px;">
<main style="flex-grow:5; background-color:#212121; color:cyan;">
main
</main>
<aside style="flex-grow:1; background-color:#666; color:yellow;">
aside
</aside>
</div>
HTML
main
Similarly, when the total length of child elements exceeds the container, you can use flex-shrink
to allow them to shrink.
The values of
flex-grow
andflex-shrink
represent the ratio in which space is added or reduced. In the example above,main
receivesof the remaining space, while aside
receives.
#Alignment on the Main Axis
By default, child elements align to the start of the main axis. You can change this behavior using the container’s justify-content
property:
start
– Align to the start of the main axisend
– Align to the end of the main axiscenter
– Center along the main axisspace-between
– Evenly distributed, no space on the endsspace-around
– Evenly distributed, with half-space on the endsspace-evenly
– Even spacing between and on the ends
Example:
<div style="display:flex; justify-content:start; height:100px; background-color:#212121;">
<div style="background-color:red;color:white;">item</div>
<div style="background-color:green;color:white;">item</div>
<div style="background-color:blue;color:white;">item</div>
</div>
<div style="display:flex; justify-content:end; height:100px; background-color:#212121;">
<div style="background-color:red;color:white;">item</div>
<div style="background-color:green;color:white;">item</div>
<div style="background-color:blue;color:white;">item</div>
</div>
<div style="display:flex; justify-content:center; height:100px; background-color:#212121;">
<div style="background-color:red;color:white;">item</div>
<div style="background-color:green;color:white;">item</div>
<div style="background-color:blue;color:white;">item</div>
</div>
<div style="display:flex; justify-content:space-between; height:100px; background-color:#212121;">
<div style="background-color:red;color:white;">item</div>
<div style="background-color:green;color:white;">item</div>
<div style="background-color:blue;color:white;">item</div>
</div>
<div style="display:flex; justify-content:space-around; height:100px; background-color:#212121;">
<div style="background-color:red;color:white;">item</div>
<div style="background-color:green;color:white;">item</div>
<div style="background-color:blue;color:white;">item</div>
</div>
<div style="display:flex; justify-content:space-evenly; height:100px; background-color:#212121;">
<div style="background-color:red;color:white;">item</div>
<div style="background-color:green;color:white;">item</div>
<div style="background-color:blue;color:white;">item</div>
</div>
justify-content:start
itemitemitem
justify-content:end
itemitemitem
justify-content:center
itemitemitem
justify-content:space-between
itemitemitem
justify-content:space-around
itemitemitem
justify-content:space-evenly
itemitemitem
#Alignment on the Cross Axis
By default, child elements stretch to fill the cross axis. This can be changed using the align-items
property:
stretch
– Stretch to fill the cross axisstart
– Align to the start of the cross axisend
– Align to the end of the cross axiscenter
– Center along the cross axis
Example:
<div style="display:flex; align-items:stretch; height:100px; background-color:#212121;">
<div style="background-color:red;color:white;">item</div>
<div style="background-color:green;color:white;">item</div>
<div style="background-color:blue;color:white;">item</div>
</div>
<div style="display:flex; align-items:start; height:100px; background-color:#212121;">
<div style="background-color:red;color:white;">item</div>
<div style="background-color:green;color:white;">item</div>
<div style="background-color:blue;color:white;">item</div>
</div>
<div style="display:flex; align-items:end; height:100px; background-color:#212121;">
<div style="background-color:red;color:white;">item</div>
<div style="background-color:green;color:white;">item</div>
<div style="background-color:blue;color:white;">item</div>
</div>
<div style="display:flex; align-items:center; height:100px; background-color:#212121;">
<div style="background-color:red;color:white;">item</div>
<div style="background-color:green;color:white;">item</div>
<div style="background-color:blue;color:white;">item</div>
</div>
align-items:stretch
itemitemitem
align-items:start
itemitemitem
align-items:end
itemitemitem
align-items:center
itemitemitem
#Wrapping
By default, when the total width of child elements exceeds the container, they overflow. You can use the flex-wrap
property to allow wrapping:
nowrap
– No wrappingwrap
– Wrap to the next line when space is insufficientwrap-reverse
– Same as wrap, but lines are reversed
Example:
<div style="display:flex; align-items:center; flex-wrap:nowrap; background-color:#212121;">
<div style="background-color:red;color:white; width:80px;">item</div>
<div style="background-color:green;color:white; width:80px;">item</div>
<div style="background-color:blue;color:white; width:80px;">item</div>
<div style="background-color:orange;color:white; width:80px;">item</div>
<div style="background-color:purple;color:white; width:80px;">item</div>
<div style="background-color:cyan;color:white; width:80px;">item</div>
</div>
<div style="display:flex; align-items:center; flex-wrap:wrap; background-color:#212121;">
<div style="background-color:red;color:white; width:80px;">item</div>
<div style="background-color:green;color:white; width:80px;">item</div>
<div style="background-color:blue;color:white; width:80px;">item</div>
<div style="background-color:orange;color:white; width:80px;">item</div>
<div style="background-color:purple;color:white; width:80px;">item</div>
<div style="background-color:cyan;color:white; width:80px;">item</div>
</div>
<div style="display:flex; align-items:center; flex-wrap:wrap-reverse; background-color:#212121;">
<div style="background-color:red;color:white; width:80px;">item</div>
<div style="background-color:green;color:white; width:80px;">item</div>
<div style="background-color:blue;color:white; width:80px;">item</div>
<div style="background-color:orange;color:white; width:80px;">item</div>
<div style="background-color:purple;color:white; width:80px;">item</div>
<div style="background-color:cyan;color:white; width:80px;">item</div>
</div>
flex-wrap:nowrap
flex-wrap:nowrapitemitemitemitemitemitem
flex-wrap:wrap
flex-wrap:nowrapitemitemitemitemitemitem
flex-wrap:wrap-reverse
flex-wrap:nowrapitemitemitemitemitemitem
#Aligning Rows on the Cross Axis
Use the align-content
property to align multiple rows on the cross axis:
start
– Align rows to the start of the cross axisend
– Align rows to the end of the cross axiscenter
– Center rows on the cross axisspace-between
– Evenly spaced rows, no space at endsspace-around
– Evenly spaced with half-space at endsspace-evenly
– Even spacing including the ends
Example:
<div style="display:flex; align-content:start; align-items:start; flex-wrap:wrap; height:100px; width:80px; background-color:#212121;">
<div style="background-color:red;color:white; width:40px;">item</div>
<div style="background-color:green;color:white; width:40px;">item</div>
<div style="background-color:blue;color:white; width:40px;">item</div>
</div>
<div style="display:flex; align-content:end; align-items:start; flex-wrap:wrap; height:100px; width:80px; background-color:#212121;">
<div style="background-color:red;color:white; width:40px;">item</div>
<div style="background-color:green;color:white; width:40px;">item</div>
<div style="background-color:blue;color:white; width:40px;">item</div>
</div>
<div style="display:flex; align-content:center; align-items:start; flex-wrap:wrap; height:100px; width:80px; background-color:#212121;">
<div style="background-color:red;color:white; width:40px;">item</div>
<div style="background-color:green;color:white; width:40px;">item</div>
<div style="background-color:blue;color:white; width:40px;">item</div>
</div>
<div style="display:flex; align-content:space-between; align-items:start; flex-wrap:wrap; height:100px; width:80px; background-color:#212121;">
<div style="background-color:red;color:white; width:40px;">item</div>
<div style="background-color:green;color:white; width:40px;">item</div>
<div style="background-color:blue;color:white; width:40px;">item</div>
</div>
<div style="display:flex; align-content:space-around; align-items:start; flex-wrap:wrap; height:100px; width:80px; background-color:#212121;">
<div style="background-color:red;color:white; width:40px;">item</div>
<div style="background-color:green;color:white; width:40px;">item</div>
<div style="background-color:blue;color:white; width:40px;">item</div>
</div>
<div style="display:flex; align-content:space-evenly; align-items:start; flex-wrap:wrap; height:100px; width:80px; background-color:#212121;">
<div style="background-color:red;color:white; width:40px;">item</div>
<div style="background-color:green;color:white; width:40px;">item</div>
<div style="background-color:blue;color:white; width:40px;">item</div>
</div>
align-content:start
itemitemitem
align-content:end
itemitemitem
align-content:center
itemitemitem
align-content:space-between
itemitemitem
align-content:space-around
itemitemitem
align-content:space-evenly
itemitemitem